import global from '../../core/global';

/**
 * 将一个input中的文件上传到服务器(支持多文件上传)
 * @param productid webtest
 * @param node fileinput结点对象
 */
export default function upload(productid: string, node: HTMLInputElement) {
	return new Promise(async (resolve, reject) => {
		const form = new FormData();
		if (node.files) {
			for (let i = 0; i < node.files.length; i++) {
				form.append('file' + i, node.files.item(i)!);
			}
			const data = await fetch(`${global.file_host}/fsweb/upload/?productid=${productid}`, {
				body: form,
				method: 'POST'
			});
			resolve(await data.json());
		} else {
			reject('请选择文件');
		}
	});
}
